/*
 * Copyright (c) 2014 Hai Bison
 *
 * See the file LICENSE at the root directory of this project for copying
 * permission.
 */

package haibison.simpleprovider.simplesettings;

import static haibison.simpleprovider.simplesettings.SimpleSettingsContract.COLUMN_KEY;
import static haibison.simpleprovider.simplesettings.SimpleSettingsContract.COLUMN_VALUE;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.text.TextUtils;

/**
 * Simple Settings.
 * 
 * @author Hai Bison
 *
 */
public class SimpleSettings {

    /**
     * This is singleton class.
     */
    private SimpleSettings() {
    }// SimpleSettings()

    /**
     * Gets value of given key.
     * 
     * @param context
     *            the context.
     * @param key
     *            the key.
     * @return the value, may be {@code null}.
     */
    public static String getValue(Context context, String key) {
        Uri uri = SimpleSettingsContract.getContentUri(SimpleSettingsContract
                .getAuthority(context));
        String[] projection = { COLUMN_VALUE };
        String selection = COLUMN_KEY + " = "
                + DatabaseUtils.sqlEscapeString(key);

        Cursor query = context.getContentResolver().query(uri, projection,
                selection, null, null);
        try {
            if (query != null && query.moveToFirst())
                return query.getString(query.getColumnIndex(COLUMN_VALUE));
        } finally {
            if (query != null)
                query.close();
        }

        return null;
    }// getValue()

    /**
     * Sets value of given key.
     * 
     * @param context
     *            the value.
     * @param key
     *            the key.
     * @param value
     *            the value.
     */
    public static void setValue(Context context, String key, String value) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_VALUE, value);

        Uri uri = SimpleSettingsContract.getContentUri(SimpleSettingsContract
                .getAuthority(context));
        String selection = COLUMN_KEY + " = "
                + DatabaseUtils.sqlEscapeString(key);
        if (context.getContentResolver().update(uri, values, selection, null) <= 0) {
            values.put(COLUMN_KEY, key);
            context.getContentResolver().insert(uri, values);
        }// if
    }// setValue()

    /**
     * Sets value of given key.
     * 
     * @param context
     *            the value.
     * @param key
     *            the key.
     * @param value
     *            the value. It will be casted to string via
     *            {@link String#valueOf(Object)}.
     */
    public static void setValue(Context context, String key, Object value) {
        setValue(context, key, String.valueOf(value));
    }// setValue()

    /**
     * Gets a long value of given key.
     * 
     * @param context
     *            the context.
     * @param key
     *            the key.
     * @param defaultValue
     *            default value if not available.
     * @return the value.
     * @throws NumberFormatException
     *             if parsing {@link String} to {@link Long} failed.
     */
    public static long getLong(Context context, String key, long defaultValue) {
        String value = getValue(context, key);
        if (TextUtils.isEmpty(value))
            return defaultValue;
        return Long.parseLong(value);
    }// getLong()

    /**
     * Gets an integer value of given key.
     * 
     * @param context
     *            the context.
     * @param key
     *            the key.
     * @param defaultValue
     *            default value if not available.
     * @return the value.
     * @throws NumberFormatException
     *             if parsing {@link String} to {@link Integer} failed.
     */
    public static int getInt(Context context, String key, int defaultValue) {
        String value = getValue(context, key);
        if (TextUtils.isEmpty(value))
            return defaultValue;
        return Integer.parseInt(value);
    }// getInt()

    /**
     * Gets a double value of given key.
     * 
     * @param context
     *            the context.
     * @param key
     *            the key.
     * @param defaultValue
     *            default value if not available.
     * @return the value.
     * @throws NumberFormatException
     *             if parsing {@link String} to {@link Double} failed.
     */
    public static double getDouble(Context context, String key,
            double defaultValue) {
        String value = getValue(context, key);
        if (TextUtils.isEmpty(value))
            return defaultValue;
        return Double.parseDouble(value);
    }// getDouble()

    /**
     * Gets a float value of given key.
     * 
     * @param context
     *            the context.
     * @param key
     *            the key.
     * @param defaultValue
     *            default value if not available.
     * @return the value.
     * @throws NumberFormatException
     *             if parsing {@link String} to {@link Float} failed.
     */
    public static float getFloat(Context context, String key, float defaultValue) {
        String value = getValue(context, key);
        if (TextUtils.isEmpty(value))
            return defaultValue;
        return Float.parseFloat(value);
    }// getFloat()

    /**
     * Gets a boolean value of given key.
     * 
     * @param context
     *            the context.
     * @param key
     *            the key.
     * @param defaultValue
     *            default value if not available.
     * @return the value.
     */
    public static boolean getBoolean(Context context, String key,
            boolean defaultValue) {
        String value = getValue(context, key);
        if (TextUtils.isEmpty(value))
            return defaultValue;
        return Boolean.parseBoolean(value);
    }// getBoolean()

}
